home *** CD-ROM | disk | FTP | other *** search
- /* Code_Main.c : file containing main() for code resource */
- /* GENERIC VERSION - modify to create your own code resources */
-
- /* Copyright © 1991, 1992 the Free Software Foundation, Inc.
- * This file is free software; you can redistribute or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 1, or any later version.
- * This file is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- * You should have received a copy of the GNU General Public License
- * along with GAWK; see the file "COPYING hAWK". If not, write to
- * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
- * Written for THINK C 4 on the Macintosh by Ken Earle (Dynabyte) Aug 1991.
- Revised May 1992: new setup/restore a4 strategy.
- */
-
- /* Required modifications: change InvokeResource to the name of the
- function that starts off your code resource. It should return an short
- as result code, and take no arguments.
- See CHANGE THIS below.*/
-
- /* Your "Invoke" function should return the following result codes:
- <= -3 : counts as -1 at present
- -2 : show stderr
- -1 : user cancelled or error during dialog - no run
- 0 : run OK, do nothing special after
- 1 : show stdout
- 2 : show and select stdout
- > 2 : no action at present (counts as equivalent to 0)
- */
-
-
- /* Files forming the basis of a new code resource:
- Source files:
- Code_Main.c
- CodeResource_Helper.c
- CodeResource_Files.c - for Macintosh-style file i/o
- Header files:
- AppCodeComm.h
- CodeResource.h
- CodeResHelper.h - prototypes for helper functions
- CodeResFiles.h - prototypes for read/write functions
- in CodeResource_Files.c
- -and a4 versions of ANSI unix etc libraries if needed.
-
- If your code resource will be large, place Code_Main.c in a segment
- by itself. The segment containing Code_Main.c also contains your
- jump table and global variables, so the code there needs to be kept
- as small as possible.
- */
-
- #include "AppCodeComm.h"
- #include <string.h>
-
- /* Global copy of the AppCodeComm struct passed from the application
- to the code resource - gacc stands for Global Application-Code resource
- Communication. */
- AppCodeComm gacc;
-
- /* Global to indicate if calling app's event loop has been made
- available and should call it */
- Boolean gConcurrent;
-
- #ifndef NULL
- #define NULL ((void *) 0)
- #endif
-
- long _code_a4, _app_a4;
-
- #define SetUpA4() asm { move.l a4,_temp_a }\
- asm { move.l _temp_c, a4 }\
- _app_a4 = _temp_a;
-
-
- #define RestoreA4() long _temp_a, _temp_c = _code_a4;\
- asm { move.l _app_a4,a4 }
- /*
- -call RestoreA4() at very beginning of callback
- -at end of each callback do SetUpA4()
- -there's also a bit of asm at the top of main() to recover a0,
- which contains the Code's a4 value, and at bottom to restore a4.
- */
-
- /* "main" call for the specific code resource */
- CHANGE THIS
- extern short InvokeResource(void); /* see ??.c */
-
- /* Protos for functions in this file */
- void main(ACCPtr ac);
- /* Callbacks, to be made available to other files */
- short InDictionary(char *tokenName);
- Boolean HasInDictionary(void);
- Handle GetFrontText(Boolean getItAll);
- Boolean HasGetFrontText(void);
- void GetNextMultiFile(short *panePtr, short *indexPtr,
- short *vRefNumPtr, char *fileName, Boolean clearFlag);
- Boolean HasGetNextMultiFile(void);
- short OKStopAlert(Ptr cstringPtr);
- Boolean HasOKStopAlert(void);
- void MemoryAlert(void);
- Boolean HasMemoryAlert(void);
- short GetScreenHeight(void);
- Boolean HasGetScreenHeight(void);
- short GetScreenWidth(void);
- Boolean HasGetScreenWidth(void);
- void SetWatchCursor(void);
- Boolean HasSetWatchCursor(void);
- void DoEventLoopOnce(void);
- Boolean HasDoEventLoopOnce(void);
- Handle GetTheClip(void);
- Boolean HasGetTheClip(void);
- short PutTheClip(char *newClipStr);
- Boolean HasPutTheClip(void);
-
- /* The "main" that is called from the running application. This file is
- kept small, and best in its own segment because lots of other things get piled
- into here as well - the jump table for the code resource, for example.
- So do the bare minimum - set up the global register reference, call the
- code resource main function, pass back a result code, and restore the
- a4 register. Functions below main() are the extension interface
- functions, which must be in this file to make use of the little functions
- above that save and restore a4. They all check to see that the extension
- in question has been passed from the application, and if not either return
- a zero or NULL or return a safe general value. No extension should be
- essential to the operation of the code resource.
- */
- /* results:
- <= -3 : counts as -1 at present
- -2 : show stderr
- -1 : user cancelled or error during dialog - no run
- 0 : run OK, do nothing special after
- 1 : show stdout
- 2 : show and select stdout
- > 2 : no action at present (counts as equivalent to 0)
- */
- void main(ACCPtr ac)
- {
- short result;
- long _temp_a, _temp_c;
-
- /* initial setup of a4 */
- asm
- {
- move.l a4, _temp_a
- move.l a0, _temp_c
- move.l a0, a4
- }
- _app_a4 = _temp_a;
- _code_a4 = _temp_c;
-
- /* Uncomment this for a version "sanity check": **************
- if (ac->version < 0 || ac->version > 100)
- {
- OKStopAlert("Version number passed by calling application \
- is impossible. Please fix the application before trying again.");
- ac->result = -1;
- asm
- {
- move.l _app_a4, a4
- }
- return;
- }
- ********** end version check */
-
- gacc = *ac; /* make a global copy */
- if (gacc.version <= 1)
- {
- gacc.DoEventLoopOnce_Ext = NULL;
- gacc.GetAppClip_Ext = NULL;
- }
- if (HasDoEventLoopOnce())
- gConcurrent = TRUE;
- else
- gConcurrent = FALSE;
-
- /* call 'true' main of code resource */
- --------------CHANGE THIS-------------
- result = InvokeResource();
-
- ac->result = result;
- /****** unload non-main segments, for example
- UnloadA4Seg(NewPtr);
- UnloadA4Seg(strcmp);*****/
- /* restore app's a4 */
- asm
- {
- move.l _app_a4, a4
- }
- /* return nothing - communication via ac->result */
- }
-
- /* Extension functions. See Call_Resource.c for details
- on what these do. EnterAct supplies them all, but your application
- doesn't have to supply any of them, and your code resource should
- not rely on any of them being available.
-
- A Boolean companion function for each extension reports whether
- the extension is available for use.
- */
-
- short InDictionary(char *tokenName)
- {
- ACCPtr laccp = &gacc; /* must use a local pointer */
- short ret;
-
- RestoreA4();
- if (laccp->InDictionary_Ext != NULL)
- ret = (*(laccp->InDictionary_Ext))(tokenName);
- else
- ret = 0;
- SetUpA4();
- return(ret);
- }
-
- Boolean HasInDictionary()
- {
- return(gacc.InDictionary_Ext != NULL);
- }
-
- Handle GetFrontText(Boolean getItAll)
- {
- ACCPtr laccp = &gacc; /* must use a local pointer */
- Handle ret;
-
- RestoreA4();
- if (laccp->GetFrontText_Ext != NULL)
- ret = (*(laccp->GetFrontText_Ext))(getItAll);
- else
- ret = NULL;
- SetUpA4();
- return(ret);
- }
-
- Boolean HasGetFrontText()
- {
- return(gacc.GetFrontText_Ext != NULL);
- }
-
- void GetNextMultiFile(short *panePtr, short *indexPtr,
- short *vRefNumPtr, char *fileName, Boolean clearFlag)
- {
- ACCPtr laccp = &gacc; /* must use a local pointer */
-
- RestoreA4();
- if (laccp->GetNextMultiFile_Ext != NULL)
- (*(laccp->GetNextMultiFile_Ext))(panePtr, indexPtr,
- vRefNumPtr, fileName, clearFlag);
- else
- *indexPtr = -1;
- SetUpA4();
- }
-
- Boolean HasGetNextMultiFile()
- {
- return(gacc.GetNextMultiFile_Ext != NULL);
- }
-
- short OKStopAlert(Ptr cstringPtr)
- {
- ACCPtr laccp = &gacc; /* must use a local pointer */
- short ret;
-
- RestoreA4();
- if (laccp->OKStopAlert_Ext != NULL)
- ret = (*(laccp->OKStopAlert_Ext))(cstringPtr);
- else
- ret = 0;
- SetUpA4();
- return(ret);
- }
-
- Boolean HasOKStopAlert()
- {
- return(gacc.OKStopAlert_Ext != NULL);
- }
-
- void MemoryAlert()
- {
- ACCPtr laccp = &gacc; /* must use a local pointer */
-
- RestoreA4();
- if (laccp->MemoryAlert_Ext != NULL)
- (*(laccp->MemoryAlert_Ext))();
- SetUpA4();
- }
-
- Boolean HasMemoryAlert()
- {
- return(gacc.MemoryAlert_Ext != NULL);
- }
-
- short GetScreenHeight()
- {
- ACCPtr laccp = &gacc; /* must use a local pointer */
- short ret;
-
- RestoreA4();
- if (laccp->GetScreenHeight_Ext != NULL)
- ret = (*(laccp->GetScreenHeight_Ext))();
- else
- ret = 342; /* minimum possible */
- SetUpA4();
- return(ret);
- }
-
- Boolean HasGetScreenHeight()
- {
- return(gacc.GetScreenHeight_Ext != NULL);
- }
-
- short GetScreenWidth()
- {
- ACCPtr laccp = &gacc; /* must use a local pointer */
- short ret;
-
- RestoreA4();
- if (laccp->GetScreenWidth_Ext != NULL)
- ret = (*(laccp->GetScreenWidth_Ext))();
- else
- ret = 512; /* minimum possible */
- SetUpA4();
- return(ret);
- }
-
- Boolean HasGetScreenWidth()
- {
- return(gacc.GetScreenWidth_Ext != NULL);
- }
-
- void SetWatchCursor()
- {
- ACCPtr laccp = &gacc; /* must use a local pointer */
-
- RestoreA4();
- if (laccp->SetWatchCursor_Ext != NULL)
- (*(laccp->SetWatchCursor_Ext))();
- SetUpA4();
- }
-
- Boolean HasSetWatchCursor()
- {
- return(gacc.SetWatchCursor_Ext != NULL);
- }
-
- void DoEventLoopOnce()
- {
- ACCPtr laccp = &gacc; /* must use a local pointer */
-
- RestoreA4();
- if (laccp->DoEventLoopOnce_Ext != NULL)
- (*(laccp->DoEventLoopOnce_Ext))();
- SetUpA4();
- }
-
- Boolean HasDoEventLoopOnce()
- {
- return(gacc.DoEventLoopOnce_Ext != NULL);
- }
-
- Handle GetTheClip()
- {
- ACCPtr laccp = &gacc; /* must use a local pointer */
- Handle ret;
-
- RestoreA4();
- if (laccp->GetAppClip_Ext != NULL)
- ret = (*(laccp->GetAppClip_Ext))();
- else
- ret = NULL;
- SetUpA4();
- return(ret);
- }
-
- Boolean HasGetTheClip()
- {
- return(gacc.GetAppClip_Ext != NULL);
- }
-
- // Note for version 3 the version is signalled by long extendID == 'VER3'
- // inside gacc -- for previous versions, the odds of this long being set
- // to exactly 'VER3' are very small.
-
- short PutTheClip(char *newClipStr)
- {
- ACCPtr laccp = &gacc; /* must use a local pointer */
- short ret = 0;
-
- RestoreA4();
- if (laccp->extendID == 'VER3' && laccp->PutAppClip_Ext != NULL)
- ret = (*(laccp->PutAppClip_Ext))(newClipStr);
- SetUpA4();
- return(ret);
- }
-
- Boolean HasPutTheClip(void)
- {
- return(gacc.extendID == 'VER3' && gacc.PutAppClip_Ext != NULL);
- }
-
-